﻿using System;
using System.ComponentModel.Composition;
using System.IO;
using EnvDTE;
using Microsoft.VisualStudio.SharePoint;
using VSLangProj;
using DefaultDeploymentStepIds = Microsoft.VisualStudio.SharePoint.Deployment.DeploymentStepIds;
using DeploymentStepIds = CodeCounsel.SharePoint.DevTools.DeploymentSteps.DeploymentStepIds;
using System.Windows.Forms;

namespace CodeCounsel.SharePoint.DevTools.ProjectExtensions
{
    [Export(typeof(ISharePointProjectExtension))]
    public class ProjectExtension
        : ISharePointProjectExtension
    {
        public void Initialize(ISharePointProjectService projectService)
        {
            projectService.ProjectMenuItemsRequested += new EventHandler<SharePointProjectMenuItemsRequestedEventArgs>(projectService_ProjectMenuItemsRequested);
            projectService.ProjectInitialized += new EventHandler<SharePointProjectEventArgs>(ProjectService_ProjectInitialized);
            projectService.ProjectPropertiesRequested += new EventHandler<SharePointProjectPropertiesRequestedEventArgs>(ProjectService_ProjectPropertiesRequested);
        }

        void projectService_ProjectMenuItemsRequested(object sender, SharePointProjectMenuItemsRequestedEventArgs e)
        {
            IMenuItem item = e.ActionMenuItems.Add("Compile for Sandbox");
            item.Click += delegate
            {
                Project project = e.Project.ProjectService.Convert<ISharePointProject, Project>(e.Project);
                VSProject langProject = project.Object as VSProject;
                Reference reference = langProject.References.Item("Microsoft.SharePoint");
                reference.Remove();
                string referencePath = Path.Combine(e.Project.ProjectService.SharePointInstallPath, @"UserCode\assemblies\Microsoft.SharePoint.dll");
                reference = langProject.References.Add(referencePath);
                ProjectUtilities.GetDTE().ExecuteCommand("Build.BuildSelection");
                reference.Remove();
                referencePath = Path.Combine(e.Project.ProjectService.SharePointInstallPath, @"ISAPI\Microsoft.SharePoint.dll");
                langProject.References.Add(referencePath);
            };
            IMenuItem copyNameItem = e.ActionMenuItems.Add("Copy Assembly Name");
            copyNameItem.Click += delegate
            {
                Clipboard.SetText(ProjectUtilities.GetAssemblyName(e.Project));
            };
        }

        void ProjectService_ProjectPropertiesRequested(object sender, SharePointProjectPropertiesRequestedEventArgs e)
        {
            ProjectProperties properties = e.Project.GetAnnotation<ProjectProperties>();
            if (properties == null)
            {
                properties = new ProjectProperties(e.Project.ProjectUserFileData);
                e.Project.Annotations.Add<ProjectProperties>(properties);
            }
            e.PropertySources.Add(properties);
        }

        void ProjectService_ProjectInitialized(object sender, SharePointProjectEventArgs e)
        {
            string[] defaultWithRecreateSiteDeploy = new string[]
            {
                DefaultDeploymentStepIds.PreDeploymentCommand,
                DefaultDeploymentStepIds.RecycleApplicationPool,
                DefaultDeploymentStepIds.RetractSolution,
                DeploymentStepIds.RecreateSite,
                DefaultDeploymentStepIds.AddSolution,
                DefaultDeploymentStepIds.ActivateFeatures,
                DefaultDeploymentStepIds.PostDeploymentCommand
            };
            string[] defaultWithRecreateSiteRetract = new string[]
            {
                DefaultDeploymentStepIds.RecycleApplicationPool, 
                DefaultDeploymentStepIds.RetractSolution
            };
            e.Project.DeploymentConfigurations.Add("Default with Recreate Site",
                defaultWithRecreateSiteDeploy, defaultWithRecreateSiteRetract);

            string[] xCopyDeploy = new string[]
            {
                DefaultDeploymentStepIds.PreDeploymentCommand,
                DefaultDeploymentStepIds.RecycleApplicationPool,
                DeploymentStepIds.CopyAssemblies,
                DeploymentStepIds.CopyFiles,
                DeploymentStepIds.InstallFeatures,
                DefaultDeploymentStepIds.PostDeploymentCommand,
            };
            string[] xCopyRetract = new string[]
            {
                DefaultDeploymentStepIds.RecycleApplicationPool, 
                DefaultDeploymentStepIds.RetractSolution
            };
            e.Project.DeploymentConfigurations.Add("X-Copy Files and Assemblies",
                xCopyDeploy, xCopyRetract);

            string[] upgradeSolutionDeploy = new string[]
            {
                DefaultDeploymentStepIds.PreDeploymentCommand,
                DefaultDeploymentStepIds.RecycleApplicationPool,
                DeploymentStepIds.UpgradeSolution,
                DefaultDeploymentStepIds.PostDeploymentCommand
            };
            string[] upgradeSolutionRetract = new string[]
            {
                DefaultDeploymentStepIds.RecycleApplicationPool, 
                DefaultDeploymentStepIds.RetractSolution
            };
            e.Project.DeploymentConfigurations.Add("Upgrade Solution",
                upgradeSolutionDeploy, upgradeSolutionRetract);
        }
    }
}